/*
 * Copyright (C) 2009 M. Hautle.
 * 
 * This file is part of TrackedBean.
 * 
 * TrackedBean is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation, either version 3 of the License, or (at your option) any later version.
 * 
 * TrackedBean is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
 * A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License along with TrackedBean. If not, see <http://www.gnu.org/licenses/>.
 */
package ch.trackedbean.validator.internal;

import java.util.*;
import ch.trackedbean.validator.*;

/**
 * Configuration for an {@link IPropertyValidator}.
 * 
 * @author M. Hautle
 */
public class Configuration implements IPropertyValidatorConfiguration {
    /** Map holding the configuration values. */
    private Map<String, Object> values = new HashMap<String, Object>();

    /** The concerned class. */
    private Class concernedClass;

    /**
     * Adds a configuration entry.
     * 
     * @param key The key
     * @param value The value
     */
    public void addEntry(String key, Object value) {
        values.put(key, value);
    }

    /**
     * Clears the configuration content.
     */
    public void clear() {
        values.clear();
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Object getObject(String name) {
        return values.get(name);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public String getString(String name, String undefined) {
        final Object val = values.get(name);
        return val != null ? val.toString() : undefined;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public boolean getBoolean(String name, boolean undefined) {
        final Object val = values.get(name);
        if (val == null)
            return undefined;
        if (val instanceof Boolean)
            return ((Boolean) val).booleanValue();
        return Boolean.parseBoolean(val.toString());
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public int getInt(String name, int undefined) {
        final Number n = getNumber(name, null);
        return n != null ? n.intValue() : undefined;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public long getLong(String name, long undefined) {
        final Number n = getNumber(name, null);
        return n != null ? n.longValue() : undefined;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public double getDouble(String name, double undefined) {
        final Number n = getNumber(name, null);
        return n != null ? n.doubleValue() : undefined;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Number getNumber(String name, Number undefined) {
        final Object val = values.get(name);
        if (val == null)
            return undefined;
        if (val instanceof Number)
            return (Number) val;
        return Double.valueOf(val.toString());
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Class getConcernedClass() {
        return concernedClass;
    }

    /**
     * Sets the concerned class.
     * 
     * @param concernedClass The class
     */
    public void setConcernedClass(Class concernedClass) {
        this.concernedClass = concernedClass;
    }
}
